//---------------------------------------------------------------------
// Authors: jachymko
//
// Description: Base class for tab expansion handlers.
//
// Creation Date: Dec 27, 2006
//
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Text.RegularExpressions;

namespace Pscx.Commands.TabExpansion
{
    abstract class TabExpansionHandler
    {
        private GetTabExpansionCommand _command;

        protected TabExpansionHandler(GetTabExpansionCommand command)
        {
            
            _command = command;
        }

        public abstract void TryExpand(string line, string word);

        protected GetTabExpansionCommand Command
        {
            get { return _command; }
        }

        protected void WriteExpansions(params string[] expansions)
        {
            _command.WriteExpansions(expansions);
        }

        protected void WriteExpansions(IEnumerable<string> expansions)
        {
            _command.WriteExpansions(expansions);
        }

        protected Collection<PSObject> Invoke(string script, object[] input, object[] args)
        {
            return Command.InvokeCommand.InvokeScript(
                script, false, 
                PipelineResultTypes.None,
                input, args);
        }

        protected Collection<PSObject> InvokeArgs(string script, object[] args)
        {
            return Invoke(script, null, args);
        }

        protected Collection<PSObject> InvokeInput(string script, object[] input)
        {
            return Invoke(script, input, null);
        }
        
        protected Collection<PSObject> Invoke(string script)
        {
            return Invoke(script, null, null);
        }

        protected PSObject InvokeReturnOne(string script, params object[] args)
        {
            Collection<PSObject> results = InvokeArgs(script, args);

            if(results.Count > 0)
            {
                return results[0];
            }

            return null;
        }

        protected CommandInfo ResolveAlias(string str)
        {
            PSObject pscmd = InvokeReturnOne("Get-Command -Type Filter,Function,Cmdlet,Alias $args[0]", str);

            if (pscmd == null)
            {
                return null;
            }

            AliasInfo alias = pscmd.BaseObject as AliasInfo;
            CommandInfo command = pscmd.BaseObject as CommandInfo;

            if (alias != null)
            {
                command = alias.ResolvedCommand as CommandInfo;
            }

            return command;
        }

        protected static bool IsMethod(PSMemberTypes member)
        {
            return 0 != (member & (PSMemberTypes.Methods | PSMemberTypes.ParameterizedProperty));
        }


    }
} 
